﻿using huqiang.Collections;
using System;
using System.Runtime.InteropServices;


namespace huqiang.Communication
{
    public class Msg
    {
        public byte DataType;
        public byte CmdType;
        public UInt16 Cmd;
        public UInt16 Error;
        public string Args;
        public byte[] Data;
        public KcpUser user;
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public struct MsgReturn
    {
        /// <summary>
        /// 数据类型
        /// </summary>
        public byte Type;
        /// <summary>
        /// 此消息的id
        /// </summary>
        public UInt32 MsgID;
        /// <summary>
        /// 此消息的某个分卷
        /// </summary>
        public byte CurPart;
        /// <summary>
        /// 此消息发送的时间戳
        /// </summary>
        public UInt16 Time;
        public static unsafe int Size = sizeof(MsgReturn);
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public struct MsgTag
    {
        /// <summary>
        /// 此消息的id
        /// </summary>
        public UInt16 MsgID;
        /// <summary>
        /// 此消息的某个分卷
        /// </summary>
        public UInt16 CurPart;
        public static unsafe int Size = sizeof(MsgReturn);
    }

    internal struct MsgInfo : IClear
    {
        /// <summary>
        /// 此消息的id 
        /// </summary>
        public UInt16 MsgID;//2
        /// <summary>
        /// 此消息的某个分卷
        /// </summary>
        public UInt16 CurPart;//2
        /// <summary>
        /// 创建的时间
        /// </summary>
        public UInt16 CreateTime;
        /// <summary>
        /// 上一次发送的时间
        /// </summary>
        public UInt16 SendTime;
        /// <summary>
        /// 发送的次数
        /// </summary>
        public int SendCount;
        /// <summary>
        /// 数据
        /// </summary>
        public byte[] data;

        public void Clear()
        {
            data = null;
        }
    }

    public class BroadcastMsg : IDisposable
    {
        /// <summary>
        /// 此消息的id
        /// </summary>
        public UInt16 MsgID;//
        /// <summary>
        /// 上一次发送的时间
        /// </summary>
        public long SendTime;
        /// <summary>
        /// 发送的次数
        /// </summary>
        public int SendCount;
        /// <summary>
        /// 数据
        /// </summary>
        public byte[][] data;

        public void Dispose()
        {
            //int c = data.DataCount;
            //for (int i = 0; i < c; i++)
            //{
            //    unsafe
            //    {
            //        ((BlockInfo*)data.Addr)[i].Release();
            //    }
            //}
            //data.Dispose();
            data = null;
        }
    }
}
